﻿using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.Repository;
using Net5CommonLib.SystemLib.TypeTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VueAndNetCoreEfApi.DtoModel.SystemAdmin;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;

namespace VueAndNetCoreEfApi.Repository.SystemAdmin
{
   public  class PositioninfoRepository :BaseRepository, IPositioninfoRepository
    {
        private IRoleDbRepository _iRoleDbRepository;
        public PositioninfoRepository(IRoleDbRepository iRoleDbRepository)
        {
            _iRoleDbRepository = iRoleDbRepository;
        }
        public PagerResult<positioninfo> PositioninfoList(SearchModel m)//显示方法
        {
            IQueryable<positioninfo> iq = SoaSystemCenterDbContext.positioninfo.AsQueryable();
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;
                string po_name = "po_name";
                if (tempDi.Keys.Contains(po_name) && !string.IsNullOrEmpty(tempDi[po_name]))
                {
                    iq = iq.Where(c => c.po_name.Contains(tempDi[po_name]));
                }
                string po_other_name = "po_other_name";
                if (tempDi.Keys.Contains(po_other_name) && !string.IsNullOrEmpty(tempDi[po_other_name]))
                {
                    iq = iq.Where(c => c.po_other_name.Contains(tempDi[po_other_name]));
                }
            }
            return Pager(iq, m);
        }
        //信息添加
        public bool positioninfoAdd(positioninfo info)//信息添加
        {
            if (info == null) return false;
            //起一个事务
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    //添加信息
                    SoaSystemCenterDbContext.Add(info);
                    info.po_createtime = DateTime.Now;
                    //保存添加的用户信息
                    SoaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();

                }
                catch (Exception ex)
                {

                    tran.Rollback();
                    //return false;
                }
            }
            return true;
        }
        //信息删除
        public bool positioninfoDelete(int po_id)//信息删除
        {
            if (po_id <0) return false;
            using(var tran=SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    var dbpo_id = SoaSystemCenterDbContext.positioninfo.FirstOrDefault(c => c.po_id == po_id);
                    if(dbpo_id==null)
                    {
                        return false;
                    }
                    //
                    string delSqlInfo = "Delete from positioninfo where po_id=@p0";
                    //SoaSystemCenterDbContext.Database.ExecuteSqlRaw
                    SoaSystemCenterDbContext.Database.ExecuteSqlRaw(delSqlInfo, dbpo_id.po_id);
                    SoaSystemCenterDbContext.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                }
            }
            return true;
        }
        //public positioninfo PositioninfoDel(int po_id)
        //{
        //    positioninfo info = SoaSystemCenterDbContext.positioninfo.FirstOrDefault(c => c.po_id == po_id);
        //    positioninfo infoDto = new positioninfo();
        //    ModelCopier.CopyModel(info, infoDto);
        //    return  info;
        //}
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool PositioninfoDel(positioninfo info)
        {
            //if(info==null) return false;
            //using (var tran= SoaSystemCenterDbContext.Database.BeginTransaction())
            //{
            //    try
            //    {
            //        var dbUser= SoaSystemCenterDbContext.positioninfo.FirstOrDefault(c => c.po_id == info.po_id);
            //        ModelCopier.CopyModel(info, dbUser, "po_id", "po_other_name", "po_createtime");
            //        string delSql = "DELETE FROM positioninfo WHERE po_id=@p0";
            //        object p = SoaSystemCenterDbContext.Database.ExecuteSqlRaw(delSql, dbUser);
            //        SoaSystemCenterDbContext.SaveChanges();
            //        tran.Commit();
            //    }
            //    catch (Exception ex)
            //    {
            //        tran.Rollback();
            //    }
            //}
            //return true;
            if (info == null) return false;
            //起一个事务
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    //添加信息
                    SoaSystemCenterDbContext.Update(info);
                    //保存添加的用户信息
                    SoaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();

                }
                catch (Exception ex)
                {

                    tran.Rollback();
                    //return false;
                }
            }
            return true;
        }
        public positioninfo PositioninfoBypo_id(int po_id)
        {
            return SoaSystemCenterDbContext.positioninfo.FirstOrDefault(c => c.po_id ==po_id);
        }

        public positioninfo PositioninfoDtoBypo_id(int po_id)
        {
            positioninfo userPo = SoaSystemCenterDbContext.positioninfo.FirstOrDefault(c => c.po_id == po_id);
            positioninfoDto userDto = new positioninfoDto();
            ModelCopier.CopyModel(userPo, userDto);
            userDto.RoleList = _iRoleDbRepository.GetRoleListByUserName(po_id);
            return userDto;
        }


    }
}
